<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
    	// Returns the search results from the postings table
    	function getPostingSearchResults() {
			$db = new Connect();

			$sql = "SELECT Posting.*, Category.cName, " .
				   "IFNULL(Posting.pCity, MemberInfo.mCity) AS city " .
				   "FROM Posting INNER JOIN Category ON Category.cID = Posting.cID " .
				   "INNER JOIN MemberInfo ON MemberInfo.mID = Posting.mID ";
			
			$sql = $sql . " WHERE Posting.status = 'garage' OR Posting.status = 'reserved'";
			
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return;
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows;
			}
    	}
    	
		// Get the garage sale dates
		function getDates() {
			$db = new Connect();
			
			$sql = "SELECT year(garageSaleStartDatetime) AS ys, " .
				   "month(garageSaleStartDatetime) AS ms, " .
				   "day(garageSaleStartDatetime) AS ds, " .
				   "hour(garageSaleStartDatetime) AS hhs, " .
				   "minute(garageSaleStartDatetime) AS mms, " .
				   "year(garageSaleEndDatetime) AS ye, " .
				   "month(garageSaleEndDatetime) AS me, " .
				   "day(garageSaleEndDatetime) AS de, " .
				   "hour(garageSaleEndDatetime) AS hhe, " .
				   "minute(garageSaleEndDatetime) AS mme " .				   
				   "FROM AdminSettings " .
				   "WHERE garageSaleEndDatetime > now()";
				   			   
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find dates
			{
				return;
			}
			else // Return date info
			{
				$rows = $db->to_array($result);
				return $rows[0];
			}
		}
    ?>
    <div id="main">
    	<p class="title">
    		Garage sale
    	</p>
<?php
	$dates = getDates();
	
	if (!isset($dates))
	{
		print "<p class=\"normal\">There is no upcoming garage sale.</p>";
	}
	else
	{
		// TODO: Format the date correctly
		print "<p class=\"normal\">The next garage sale will be on " . $dates['ys'] . "-" . str_pad($dates['ms'], 2, "0", STR_PAD_LEFT) . "-" . str_pad($dates['ds'], 2, "0", STR_PAD_LEFT) . " " . str_pad($dates['hhs'], 2, "0", STR_PAD_LEFT) . ":" . str_pad($dates['mms'], 2, "0", STR_PAD_LEFT) . ".</p>";
		
		$rows = getPostingSearchResults();
		
		if (!isset($rows))
		{
			print "No results found.";
		}
		else
		{
			print "<table border=\"0\">";
			print "<col width=\"120px\">";
			print "<col width=\"100px\">";
			print "<col width=\"200px\">";
			
			$rowcount = count($rows);
			for ($i = 0; $i < $rowcount; $i++)
			{
				// TODO: Output the results of the search on the page
				print "<tr>";
				print "<td colspan=\"3\" class=\"normal\"><a href=\"posting.php?pid=" . $rows[$i]['pID'] . "\">" . $rows[$i]['pName'] . "</a></td>";
				print "</tr>";
				print "<tr>";
				print "<td rowspan=\"4\" class=\"normal\">";
				print "<a href=\"posting.php?pid=" . $rows[$i]['pID'] . "\">";
				print "<img src=\"" . $rows[$i]['img'] . "\" height=\"100px\" width=\"100px\" alt=\"Image\"/></a></td>";
				print "</tr>";
				print "<tr>";
				print "<td class=\"normal\">" . $rows[$i]['cName'] . "</td>";
				print "<td class=\"normal\">" . $rows[$i]['pDatetime'] . "</td>";
				print "</tr>";
				print "<tr>";
				print "<td class=\"normal\">" . $rows[$i]['price'] . "$</td>";
				print "<td class=\"normal\">" . $rows[$i]['city'] . "</td>";
				print "</tr>";
				print "<tr>";
				print "<td class=\"normal\">&nbsp</td>";
				print "</tr>";
			}
			
			print "</table>";
		}
	}
?>
    </div>
</body>
</html>